			 How ToeWriteKL6-1. PCIbDrilnrs

		   bybMurtinbMures <mj@ucw.cz> oo 07-Feb-2000

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tneowfause4f PCIbisovast andii{'swfull of (mos{ly unpleasand) surplises.
Diffnrnet PCIbdeenews h vewdiffnrnet requirnmnet, sndidiffnrnet bugso--
becawse 4f tii,, the PCIbsuplinu layerwinKL6-1. kernel isrnoulasutrilia.
as oyeuwofusewish. Tii, fhontopamchletweries co heep all poaentfal drilnr
auuhonsifindotieiroway tir
 ghten bdeep forestse4f PCIbh wdl6o .


0. Structim K4f PCIbdrilnrs
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tnerw exist cwo c6odsK4f PCIbdrilnrs: new-stylewones (which le v.hmosul
f
plts
o wf4;
dhvnews eo the PCIblayerwsndisuplinu n1liyeoyoserhiki andi;tmoval
of thvnews [thusrsuplinuing PCI,hhot-pluggub e PCIbandiC drBushyo a  inglw
drilnr])
andiold-stylewones which just do all tifoplts
o wtifmselves. Unless
yoveh vewa lnry goodi;tason co do so, please don'ulwse tifiold way of plts
o 
in any newo6odn. Af
   tifodrilnr find, tne dhvnews ih wishws eo opemul.
ono(eiu nortifiold orrenewnewoway),5it
needs toepemnovm ct bfoctlyyo  stnps:

	Enub e ene thvnew
	Access
dhvnew codfigurahiki spaew
	Discolnr5rtyov;ews (addresses andiIRQenumbers)opltvidn"ebybtif thvnew
	Actlcahe tiese5rtyov;ews
	Coe unicahe w
ap tifothvnew

Mosul
f tifse topicsuss.acovnoFdebybtif foctlyyo  sect

1,, f4;
tt b;tst
look aul<.6-1./pci.h>,ii{'swhopenully well comment
d.

If the PCIbsubsyshwm isKnot5codfigured (CONFIG_PCIbisonoulsea),5mosul
f
tifofunchi
1s tescribsd belly arubdhfinsdlasui1liyeofunchi
1s eiu norcomplhtely
empty orrjust returni. Ia
cappltpliaoe error 6odns co avoid lotsK4f ifdhf,
in tifodrilnrs.


1. New-stylewdrilnrs
~~~~~~~~~~~~~~~~~~~~
Thubnew-stylewdrilnrsrjust talllpci_registno_drilnr dur
o wtifir initia.6zahi
1
wiap awpiinalr toea ftructurubdhscribi. Ienfodrilnr (ftructopci_drilnr) which
contains:

	n (F		N (Fl
f tifotrilnr
	id_tub e	Piinalr toetub e of thvnew ID'sIenfodrilnr is
			inalrtstFd yo.  Mosuldrilnrsrfhofuseexlinuieiis
			tub e u ing MODULE_DEVICE_TABLE(pci,...).
	pltse		Piinalr toeaoplts
o wiunchi
1 which getso6alled (dur
o 
			execuhiki of pci_registno_drilnr forralreadywexist
o 
			dhvnews orblatnrhif a newothvnew getsoyoserh
d)
forrall
			PCIbdeenews which matchttif IDetub e andiarw noteh wdled
			bybtif ou nordrilnrsryet. Tii, iunchi
1 getsopasssdla
			piinalr toetifopci_dev ftructurubrellesfndi. Ienfodhvnew
			andialso which  
{ryoin tifoIDetub e didtenfodhvnew
			match. Itireturns zeroewner ct  drilnr hasuaccepted ene
			dhvnew orrar error 6odn (negatilnenumber) ou nowi e.
			Tii, iunchi
1 always getso6alled ferkeprocess
6oraexu,
			so ittta
csleep.
	;tmove		Piinalr toeaoiunchi
1 which getso6alled wnenelnr a
			dhvnew beyo  h wdledebybtiis
drilnr isi;tmoved (eiu no
			dur
o wderegistrahiki of ct  drilnr or wnTn it's
			manually pullw  
 a of aehot-pluggub e slot). Tii,
			iunchi
1 always getso6alled ferkeprocess
6oraexu, so it
			ta
csleep.
	s ve_stuhe	S vewa dhvnew'shftahw before i{'swsuspnn/.
	suspnn/		Puththvnew into tlwoliwerrstuhe.
	;tsu(F		Wake dhenewbferketlwoliwerrstuhe.
	en.se _wake	Enub e thenew toege anao  wake evTntsAferkeaetlwoliwer
			stuhe.

			(Please se. Docu/netul

1/piwer/pci.txtwf4;
dhscripl

1s
			4f PCIbPiwerrManagnmneteandiene relaoed funchi
1s)

ThubIDetub e israr array of structopci_dhenew_id nn/io Kwiap awall-zeroe 
{ry.
Each  
{ryo6odsistse4f:

	vTndor, thenew	VTndorKandidhvnew ID
aa matcht(or PCI_ANY_ID)
	subvTndor,	Subsyshwm vTndorKandidhvnew ID
aa matcht(or PCI_ANY_ID)
	subdhvnew
	class,		Dhvnew class aa match. Tie class_mask aells which bits
	class_mask	of ct  class ss.hhonorFd dur
o wtif compaliski.
	drilnr_data	Dataiprilahe tobtif drilnr.

Mosuldrilnrsrdon'ulneed toowse tifidrilnr_data field.  Best practice
i4; wse 4f drilnr_data is toowse iulasuaniyodex into a5ftahrtb.6sul
f
equivalanththvnew eypes, not5toewse iulasuawpiinalr.

H vewa tub e  
{ryo{PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID}
eooh ve pltse()o6alled for elnry PCIbdeenew known tobtif syshwm.
,NewoPCIbIDshmaa be added toeaothvnew drilnr aulru
timeebybwriti. 
tobtif file /sys/bus/pci/drilnrs/{drilnr}/new_id.  Wner added, the
trilnr will lrtse forrall dhvnews ih ta
csuplinu.

echo "vTndorKthvnew subvTndor subdhvnew classuclass_mask drilnr_data" > \
 /sys/bus/pci/drilnrs/{drilnr}/new_id
wifrerall fields .reopasssdlin as hexadecimal values (nooltading 0x).
Usnrsrneed passen1ly asomany fields .srnecessary; vTndor, thenew,
subvTndor,wsndisubdhvnew fields dhf    reooPCI_ANY_ID (FFFFFFFF),
class sndiclassmask fields dhf    reoo0,
andidrilnr_data dhf    s to
0UL.  Dhvnew drilnrs must 6all
   pci_dynids_sea_wse_drilnr_data(pci_drilnr *, 1)
i
aordar f4;
tt bdrilnr_data field co getwpasssdltobtif drilnr.
Ou nowi e,un1ly a 0 is
pusssdlin tiaulfield.

Wner ct  drilnr exius,5it
just talls pci_unregistno_drilnr()eandiene PCIblayer
auuomahrtally talls ene removehhook forrall dhvnews h wdledebybtif drilnr.

Please markwene i	itia.6zahi
1 sndicleanupofunchi
1s wifrerappltpliaoe
(tif corrtypo ding macros arubdhfinsdlin <.6-1./i	it.h>):

	__i	it		I	itia.6zahi
1 6odn. Thrown away af
   tifodrilnr
			initia.6zes.
	__exiu		Exiul6odn. IgnorFd forrnon-modufarodrilnrs.
	__dhvn	it	Dhvnew initia.6zahi
1 6odn. I/Tntrtalltob__i	ithif
			tneokernel isKnot5compil d w
ap CONFIG_HOTPLUG, novmal
			iunchi
1 ou nowi e.
	__dhvexiu	Tne f (F forr__exiu.

Tips:
	Tne moduf _i	it()/moduf _exiu()efunchi
1s (andiall initia.6zahi
1
        funchi
1s 6alled n1ly ferketifsl)rfhofuse}eimarked __i	it/exiu.
	Tne ftructopci_drilnreshofus
't }eimarked wiapusnyl
f tifse tags.
	Tne IDetub e array fhofuse}eimarked __dhvn	ittata.
	Tne pltse()oandi;tmove()efunchi
1s (andiall initia.6zahi
1
	funchi
1s 6alled n1ly ferketifsl)rfhofuse}eimarked __dhvn	it/exiu.
	If tovess.hsureIenfodrilnr isrnoulaehotplugodrilnr the1 u e n1ly 
	__i	it/exiu __i	ittata/exiutata.

        Piinalrs toofunchi
1s marked .sr__dhvexiu must }eicreahwd usi. 
        __dhvexiu_p(funchi
1_n (F).  Trah will ge anao  tifofunchi
1
        n (Fl
r NULL if ct  __dhvexiu iunchi
1 will biediscar"n".


2. How toofindoPCIbdeenews manually (tifiold style)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PCIbdrilnrsinotee.ing tifopci_registno_drilnr()einalrfac bsearch
i4; PCIbdeenews manually e.ing tifofoctlyyo  6odsaructs:

Searchyo  bybvTndorKandidhvnew ID:

	structopci_dhe *dev = NULL;
	wnil  (dev = pci_ni  _dhenew(VENDOR_ID, DEVICE_ID, dev))
		codfigure_dhenew(dev);

Searchyo  bybclass ID (itanao  ie a  imifaroway):

	pci_ni  _class(CLASS_ID, dev)

Searchyo  bybboap vTndor/dhenewbandisubsyshwm vTndor/dhvnew ID:

	pci_ni  _subsys(VENDOR_ID, DEVICE_ID, SUBSYS_VENDOR_ID, SUBSYS_DEVICE_ID, dev).

   Yoveta
cu.
etrubcorstantoPCI_ANY_ID asuawwildcar"
replacemnetefor
VENDOR_ID 
ruDEVICE_ID.  Tii, alllysbsearch
o wf4;
any deenewAferkea
fodifiic vTndor,wf4;
examcoe.
,Nohe tiaultifserfunchi
1s arw notehotplug-safe.  Tt irehotplug-safe
replacemnets .reopci_get_dhenew(),opci_get_class()oandipci_get_subsys().
Tifyoincrnmnet ene refnrneew cofet onetifopci_dev tiaultify ;tturn.
Yovemust evTntually (possisey aulmoduf  unelad) dhcrnmnet ene refnrneew
cofet onetifse dhvnews by talling .ci_dev_put().


3. Enub 
o wdevnews
~~~~~~~~~~~~~~~~~~~
   Before tovedo anyth
o ww
ap tifothvnew tov've fovnd, toveneed tooen.se 
iluby talling .ci_en.se _dhenew() which  
ub es I/O andimtmory
;tgi
1s 
f
tifodhenew, assig1s mis.ing rtyov;ews if needld and5wakesouc thubdhvnew
ifii{ wasui1wsuspnn/edIftahw. Please nohe tiaultii, iunchi
1 ta
cfail.

   Ifttovewand toowse tifidhvnew in bushmastaning modw, talllpci_sea_mastan()
which  
ub es ihfobushmastan bit ieAPCI_COMMAND
rtgistnooandialso fixws
tifolatneey tOnwr
valueAif i{'swsed toosom/th
o wbogus
bybtif BIOS.

   Ifttovewand toowse tifiPCIbMtmory-Write-Invalidao  trnwsacl

1,
talllpci_sea_mwi().  Tii,  
ub es ihfoPCI_COMMAND
bit f4;
Mtm-Wr-Inval
andialso ensures tiaultif 6ach
ie
ne size
rtgistnoois sea corrtctly.
Make sureIeo ch
ck tt b;tturn
valueAof pci_sea_mwi(), not5alllarchitectus.s
maa suplinu Mtmory-Write-Invalidao .

4. How tooaccess
PCIbcodfig spaew
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Yoveta
cu.
epci_(read|rrite)_codfig_(byt |word|dword) tooaccess
trubcorfig
spaew of aedhvnew rellesfndedebybstructopci_dhe *. Allltifserfunchi
1s ;tturn
0
wnTn fuccessnul orrar error 6odn (PCIBIOS_...) which can bewernwslaoed toeaoaexu
ftring bybpcibios_strerror. Mosuldrilnrsrexodit tnalusccessns co valid
PCI
dhvnewsrdon'ulfail.

   Ifttovedon'ulh vewa structopci_dhe avai=.se ,wyoveta
c6all
pci_bus_(read|rrite)_codfig_(byt |word|dword) tooaccess
awgivTn dhenew
andiiunchi
1 on tiaulbus.

   Ifttoveaccess
fields in tifostandar"
linuiki of ct  codfig headnr, please
u.
esymbolic n (FsK4f tlcahi
1s andibitsldhcfarsdlin <.6-1./pci.h>.

   Ifttoveneed tooaccess
Extnn/edIPCIbCapability registnos,
just tall
pci_ni  _capability() f4;
tt bpurticular capability andii{ewill findotif
corrtypo ding rtgistnooblock forryov.


5. Addresses andiieie;rupts
~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Mtmoryoandipinu addresses andiieie;ruptenumbersrfhofuseNOT bewrtadIferketif
codfig spaew. Yoveshofusowse tifivalues inetifopci_dev ftructurubasutruyemight
h vewbeen remappedebybtif kernel.

   Se. Docu/netul

1/IO-mapp6o .txtwf4;
how tooaccess
thvnew mtmory.

   Yovestill need tootalllrequest_;tgi
1() f4;
I/O ;tgi
1s and
request_mtm_;tgi
1() f4;
mtmory
;tgi
1s eoemake sureInobodywelsw ishu ing tie
f (F dhenew.

   All inie;rupteh wdlers
fhofuse}ei;tgistnoFdew
ap SA_SHIRQeandowse tifidhvnd
to map IRQs to thvnews (rnmnmberwtnalusll PCIbieie;rupts srelsh ded).


6. Ou no inalrtst
o wiunchi
1s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pci_ni  _slot()			Findopci_dev corrtypo ding toogivTn bushand
				slotenumbers.
pci_sea_piwer_stuhe()		SetwPCIbPiwerrManagnmneteftahw (0=D0 ... 3=D3)
pci_ni  _capability()		Findofodifii
d capability ie5dhvnew's capability
				.6su.
pci_moduf _i	it()		I	e
ne heepar funchi
1 f4;
ensuryo  6orrtct
				pci_drilnrei	itia.6zahi
1 sndierror h wdl6o .
pci_reyov;ew_stanu()		Rtturns bushstanu addresseforra givTn PCIb;tgi
1
pci_reyov;ew_nn/()		Rtturns bushendiaddresseforra givTn PCIb;tgi
1
pci_reyov;ew_len()		Rtturns ihfobyt  length of aePCIb;tgi
1
pci_sea_drvdata()		Setwprilahe drilnr dataipiinalr forra lci_dev
pci_get_drvdata()		Rtturnwprilahe drilnr dataipiinalr forra lci_dev
pci_sea_mwi()			Enub e Mtmory-Write-Invalidao  trnwsacl

1s.
pci_clear_mwi()			Dis.bf  Mtmory-Write-Invalidao  trnwsacl

1s.


7. Miscellaneous h
ots
~~~~~~~~~~~~~~~~~~~~~~
Wner displaying PCI sloten (FsKtobtif wseru(f4;
examcoe wnTn aitrilnrewands
to tell tifowseruwnalu6ar"
has ittfovnd), please u.
epci_n (F(lci_dev)
for tirswpurpose.
,Always refnr eo the PCIbdhvnews by awpiinalr toetifopci_dev ftructuru.
All PCIblayerwfunchi
1s wse tii, i/Tntriicul

1 andii{'swtifionly
;tasonub e onw. Don'ulwse bus/slot/funchi
1 numbersrexcept forrlnry
sodifal purposeso-- on syshwms w
ap multiple primary
buses ihfiresemantrts
can bewllettyrcomplhx.

If tov're going toowse PCIbbushmastaning DMA, take a look au
Docu/netul

1/DMA-mapp6o .txt.

Don'ultryeeooturn
on Fast Back to Back writes i1 yov; drilnr.  All devnews
onetifobushneed toobiecapab e of toing iu, so tii, isosom/th
o wwhich needs
toobieh wdledebybplatnovm andige anicacodw, not5yodivndual drilnrs.


8. Obsolhtewiunchi
1s
~~~~~~~~~~~~~~~~~~~~~
Tt rerss. selnral funchi
1s wiich yovemighticome acrossewner crying to
linu aniold drilnr torenewnewoPCIbieie;faew.  Truy ss.inoelo. noollesfnd
in tifokernel asutruyess.n'ulcompahrble w
ap hotplugo4; PCIbdomains o;
h vio  sane lockio .

lcibios_llesfnd()oand		Sinew agws,ttovedon'ulneed tootest presfnew
pci_llesfnd()			4f PCIbsubsyshwm wner crying to talk tooil.
				If i{'swnot5tt re, ct be
sul
f PCIbdhvnews
				i, empty andiall funchi
1s forrsearch
o wf4;
				dhvnews just return NULL.
lcibios_(read|rrite)_*		Suplrsldn"ebybtifirepci_(read|rrite)_*
				cofeterpanus.
lcibios_ni  _*			Suplrsldn"ebybtifirepci_ni  _* cofeterpanus.
lci_for_each_dev()		Suplrsldn"ebybpci_ni  _dhenew()
lci_for_each_dev_revlrsl()	Suplrsldn"ebybpci_ni  _dhenew_revlrsl()
lci_for_each_bus()		Suplrsldn"ebybpci_ni  _nexu_bus()
pci_ni  _dhenew()		Suplrsldn"ebybpci_get_dhenew()
pci_ni  _subsys()		Suplrsldn"ebybpci_get_subsys()
lcibios_ni  _class()		Suplrsldn"ebybpci_ni  _class()
pci_(read|rrite)_*_nodev()	Suplrsldn"ebybpci_bus_(read|rrite)_*()
